#!/bin/bash

source ${CAVAN_HOME}/script/core/application.sh

ARCH="arm"
CROSS_COMPILE="arm-eabi-"
MAKE="make -j${CPU_CORE_NUM}"
MAKE_CROSS="${MAKE} ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE}"
MAKE_KERNEL="${MAKE_CROSS} LOADADDR=0x10008000"
UBOOT_PATH="bootable/bootloader/uboot-imx"
KERNEL_DEFCONFIG="imx_v7_android_defconfig"

export ARCH CROSS_COMPILE

BUILD_TOP="$(pwd -P)"
GIT_PATH="${BUILD_TOP}/.git"
BOOT_EXTRA="${GIT_PATH}/boot"
FILE_BOOT_RAW="${GIT_PATH}/boot-raw.img"
FILE_BOOT_TARGET="${GIT_PATH}/boot.img"
FILE_ZIMAGE="${BUILD_TOP}/arch/arm/boot/zImage"
FILE_DTB="${BUILD_TOP}/arch/arm/boot/dts/imx6dl-ms600.dtb"

CAVAN_ANDROID_ROOT=$(cavan-get-android-root) || CAVAN_ANDROID_ROOT="${ANDROID_BUILD_TOP}"
[ "${TARGET_PRODUCT}" = "imx6ms600" ] ||
{
	cavan-set-jdk-version 1.6 || exit 1

	cd "${CAVAN_ANDROID_ROOT}" || exit 1
	source build/envsetup.sh || exit 1
	lunch imx6ms600-user || exit 1
	cd - || exit 1
}

function error_exit()
{
	[ -f "${FILE_ERR}" ] && cat "${FILE_ERR}"
	exit 1
}

function exec_command()
{
	echo -e "\033[33mExecute: $1\033[0m"
	eval "$1" && return 0

	echo -e "\033[31mFailed to execute: $1\033[0m"
	error_exit
}

function build_uboot()
{
	exec_command "${MAKE_CROSS} distclean"
	exec_command "${MAKE_CROSS} mx6dlms600${1}_config"
	exec_command "${MAKE_CROSS}"
}

function repack_boot()
{
	rm -rf "${BOOT_EXTRA}" || return 1
	mkdir -pv "${BOOT_EXTRA}" || return 1
	cd "${BOOT_EXTRA}" || return 1
	cavan-bootimg unpack "${FILE_BOOT_RAW}" || return 1
	cp -av "${FILE_ZIMAGE}" kernel.bin || return 1
	cp -av "${FILE_DTB}" second.bin || return 1
	cavan-bootimg pack || return 1
	cp -av boot.img "${FILE_BOOT_TARGET}" || return 1

	return 0
}

if [ -f "build/envsetup.sh" ]
then
	CONFIG_BUILD_UBOOT="true"
	CONFIG_BUILD_KERNEL="true"
	CONFIG_BUILD_ANDROID="false"

	LOG_PATH="${ANDROID_BUILD_TOP}/log"
	FILE_ERR="${LOG_PATH}/ms600-err.txt"
	FILE_OUT="${LOG_PATH}/ms600-out.txt"

	echo "FILE_ERR = ${FILE_ERR}"
	echo "FILE_OUT = ${FILE_OUT}"

	exec_command "mkdir -pv ${OUT}"
	exec_command "mkdir -pv ${LOG_PATH}"

	[ -f "${UBOOT_PATH}/u-boot.imx" ] && CONFIG_BUILD_UBOOT="false"
	[ -f "${OUT}/boot.img" ] && CONFIG_BUILD_KERNEL="false"

	case "$1" in
		uboot|u-boot.*)
			CONFIG_BUILD_UBOOT="true"
			CONFIG_BUILD_KERNEL="false"
			;;
		kernel|boot|boot.img)
			CONFIG_BUILD_UBOOT="false"
			CONFIG_BUILD_KERNEL="true"
			;;
		android|system|system.img)
			CONFIG_BUILD_ANDROID="true"
			;;
		all)
			CONFIG_BUILD_UBOOT="true"
			CONFIG_BUILD_KERNEL="true"
			CONFIG_BUILD_ANDROID="true"
			;;
		*)
			[ -z "$1" ] || exit 1
			CONFIG_BUILD_ANDROID="true"
			;;
	esac

	{
		[ "${CONFIG_BUILD_UBOOT}" != "true" ] || (cd ${UBOOT_PATH} && build_uboot android) || error_exit
		cp -av ${UBOOT_PATH}/u-boot.imx ${OUT}/u-boot.imx || error_exit
		cp -av ${UBOOT_PATH}/u-boot.imx ${OUT}/u-boot-imx6dl.imx || error_exit
		[ "${CONFIG_BUILD_KERNEL}" != "true" ] || exec_command "${MAKE} bootimage"
		[ "${CONFIG_BUILD_ANDROID}" != "true" ] || exec_command "${MAKE}"
	} 2>${FILE_ERR} | tee ${FILE_OUT}
elif [ -f "include/linux/kernel.h" ]
then
if [ -d "${CAVAN_ANDROID_ROOT}" ]
then
	(cd ${CAVAN_ANDROID_ROOT} && exec_command "${MAKE} boot.img") || exit 1
	cp -av "${OUT}/boot.img" "${FILE_BOOT_TARGET}" || exit 1
else
	[ -f ".config" ] || exec_command "${MAKE_KERNEL} ${KERNEL_DEFCONFIG}"
	exec_command "${MAKE_KERNEL} uImage"
	exec_command "${MAKE_KERNEL} dtbs"
	(repack_boot) || exit 1
fi
elif [ -f "boards.cfg" ]
then
case "$1" in
	mfg)
		unset UBOOT_TARGET
		;;
	android)
		UBOOT_TARGET="android"
		;;
	*)
		[ -z "$1" ] || exit 1
		UBOOT_TARGET="android"
		;;
esac
build_uboot ${UBOOT_TARGET}
else
exit 1
fi
